package com.mydigitallife.timetracker;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemLongClickListener;

public class overview extends ListActivity { 
	
	public void onCreate(Bundle savedInstanceState) {
		  super.onCreate(savedInstanceState);
		  
			SQLiteDatabase myDB = null;
			myDB = this.openOrCreateDatabase("TimeTrack", MODE_WORLD_READABLE, null);
			
			
			Cursor cursor = myDB.rawQuery("SELECT projects._id, projects.projectname, tracking.starttime, tracking.endtime, round(strftime(\"%s\", tracking.endtime) - strftime(\"%s\", tracking.starttime))/3600 AS duration FROM projects, tracking  ON tracking._id = projects._id;", null);
			
			ListAdapter adapter = new SimpleCursorAdapter(
					this,
					R.layout.overview,
					cursor,
					new String[]{"projectname","starttime","endtime","duration"},
					new int[]{R.id.projectname,R.id.projectstart,R.id.projectend,R.id.projectduration});		  
		  
			setListAdapter(adapter);

		  final ListView lv = getListView();
		  lv.setTextFilterEnabled(true);
		  
		  lv.setOnItemLongClickListener(new OnItemLongClickListener(){

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int posistion, long id) {
				changeData(view, posistion+1);
				return false;
			}
		  });
		  myDB.close();
		}
	
	private void changeData(View v, int id){
		Intent classSwitch = new Intent(v.getContext(), changeData.class);
		classSwitch.putExtra("test", id);
		startActivityForResult(classSwitch, 0);
	}
	
}
